// pages/member/index.js
var app = getApp();
var base = require('../../conf.js')
var interval = null;
Page({
  data: {
    user:{},
    max_max: 888000,
    show:false,
    max:5000,
    vip0: 'http://pbr2drfuy.bkt.clouddn.com/Vip1%20%288%29.png',
    vip1: 'http://pbr2drfuy.bkt.clouddn.com/Vip2%20%282%29.png',
    vip2: 'http://pbr2drfuy.bkt.clouddn.com/Vip3%20%282%29.png',
    vip3: 'http://pbr2drfuy.bkt.clouddn.com/Vip4%20%282%29.png',
    vip4: 'http://pbr2drfuy.bkt.clouddn.com/vip5.png',
    vip5: 'http://pbr2drfuy.bkt.clouddn.com/Vip6%20%282%29.png',
    surp:5000,
    isDisplay:true,
    phone: '',
    code:'',
    canClick: false,
    time: '',
    disabled: false,
    currentTime:60,
    isvip0: false,
    isvip1: false,
    isvip2: false,
    isvip3: false,
    isvip4: false,
    isvip5: false,
    isvip6: false,
    level_divide: {
      vip0: 0,
      vip1: 0,
      vip2: 0,
      vip3: 0,
      vip4: 0,
      vip5: 0,
      vip6: 0,
      vip7: 0
    },
  },
  onLoad(){
    this.getUser()
    this.getUserPhone()
  },
  turnUserInfo: function () {
    wx.navigateTo({
      url: '/pages/member/person',
    })
  },
  scoreWithdraw: function () {
    wx.navigateTo({
      url: '/pages/member/scoreWithdraw',
    })
  },
  Show: function () {
    var that = this;
    that.setData({
      show: (!that.data.show),
    })
  },
  turnPay: function () {
    wx.navigateTo({
      url: '/pages/member/collection',
    })
  },
  turnVoucher: function () {
    wx.navigateTo({
      url: '/pages/member/surprise',
    })
  },
  turnPresentRecord: function () {
    wx.navigateTo({
      url: '/pages/member/withdrawRecord',
    })
  },
  turnOrderRecord: function () {
    wx.navigateTo({
      url: '/pages/member/orderRecord',
    })
  },
  turnShareModel: function () {
    wx.navigateTo({
      url: '/pages/member/person',
    })
  },
  turnUser: function () {
    wx.navigateTo({
      url: '/pages/member/Share',
    })
  },
  turnCooperation: function () {
    wx.navigateTo({
      url: '/pages/member/person',
    })
  },
  call: function () {
    wx.makePhoneCall({
      phoneNumber: '0731-82274665',
    })
  },
  toOrder: function () {
    wx.navigateTo({
      url: "/pages/home-page/order"
    })
  },
  disDate: function() {
    if (!this.isDisable) {
      this.isDisable = 'disabled'
    } else {
      this.isDisable = false
    }
  },
  getUser:function(){
    var self = this;
    wx.request({
      url: base.url + '/userInfo',
      method: 'get',
      header: {
        "Content-Type": "application/json;charset=UTF-8",
        'userToken': base.token_Date
      },
      success: function (res) {
        console.log(res.data.data)
        self.setData({
          user: res.data.data
        })
        //self.getLevelDivide()
      }
    })
  },
  getLevelDivide:function(){
    var self = this;
    wx.request({
      url: base.url + '/levelNumber',
      method: 'get',
      header: {
        "Content-Type": "application/json;charset=UTF-8",
        'userToken': base.token_Date
      },
      success: function (res) {
        console.log(res.data.data)
        let json = {
          vip0: res.data.data[0].number,
          vip1: res.data.data[1].number,
          vip2: res.data.data[2].number,
          vip3: res.data.data[3].number,
          vip4: res.data.data[4].number,
          vip5: res.data.data[5].number,
          vip6: res.data.data[6].number,
          vip7: res.data.data[6].endNumber
        }
        self.setData({
          level_divide : json,
          max : this.level_divide.vip0
        })
        self.level_sele();
      }
    })
  },
  getUserPhone: function() {
    var self = this;
    wx.request({
      url: base.url + '/getPhone',
      method: 'get',
      header: {
        "Content-Type": "application/json;charset=UTF-8",
        'userToken': base.token_Date
      },
      success: function (res) {
        self.setData({
          userPhone: res.data.data,
        })
        if (self.data.userPhone.phone == null || self.data.userPhone.phone == "") {
          self.setData({
            isDisplay : true
          })
        } else {
          self.setData({
            isDisplay : false
          })
        }
      }
    })
  },
  mobileInput: function (e) {
    this.setData({
      phone: e.detail.value
    })
  },
  getUpdateUser() {
    var phone = this.data.phone;
    var code = this.data.code;
    var phonetel = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if (phone == '') {
      wx.showToast({
        title: '手机号不能为空',
        icon: 'loading',
      })
      return false
    }
    else if (phone.length != 11) {
      wx.showToast({
        title: '手机号长度有误！',
        icon: 'loading',
        duration: 1500
      })
      return false;
    }
    else if (code == '') {
      wx.showToast({
        title: '请输入验证码！',
        icon: 'loading',
        duration: 1500
      })
      return false;
    }

    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if (!myreg.test(phone)) {
      wx.showToast({
        title: '手机号有误！',
        icon: 'loading',
        duration: 1500
      })
      return false;
    }
    return true;

  },
  Yzm: function () {
    var phone=this.data.phone;
    var isPhone = /^1[34578]\d{9}$/
    if (isPhone.test(phone)) {
      var that = this;
      that.setData({
        canClick: (!that.data.canClick),
      })
      var currentTime = that.data.currentTime;
      interval = setInterval(function () {
        currentTime--;
        that.setData({
          time: currentTime + '秒后重新发送'
        })
        if (currentTime <= 0) {
          clearInterval(interval)
          that.setData({
            //time: '重新发送',
            currentTime: 61,
            canClick: (!that.data.canClick),
          })
        }
      }, 1000)
      var params = { "phone": that.data.phone }
      console.log(params)
      wx.request({
        url: base.url + '/getCode',
        method: 'get',
        header: {
          "Content-Type": "application/json;charset=UTF-8",
          'userToken': base.token_Date
        },
        data: { params: params},
        success: function (res) {
          console.log(res.data.data)
          that.setData({
            userCode : res.data.data
          })
        },
        fail: function (res){
          console.log(res)
        }
      })
    }
    else {
      wx.showToast({
        title: '手机号有误！',
        icon: 'loading',
        duration: 1500
      })
    }
  },
  turn_yue: function () {
    wx.navigateTo({
      url: "/pages/pic/yue"
    })
  },
  turn_cz: function () {
    wx.navigateTo({
      url: "/pages/pic/cz"
    })
  },
  turn_level: function () {
    wx.navigateTo({
      url: "/pages/pic/level"
    })
  },
  turn_fx: function () {
    wx.navigateTo({
      url: "/pages/pic/fx"
    })
  },
  turn_cooperation: function () {
    wx.navigateTo({
      url: "/pages/pic/cooperation"
    })
  },
  level_sele() {
    this.isvip0 = false,
    this.isvip1 = false,
    this.isvip2 = false,
    this.isvip3 = false,
    this.isvip4 = false,
    this.isvip5 = false,
    this.isvip6 = false,
    this.max_max = this.level_divide.vip6
    if (this.user.growth >= this.level_divide.vip0 && this.user.growth < this.level_divide.vip1) {
      this.isvip0 = true,
      this.max = this.level_divide.vip1,
      this.mygrowth = this.user.growth * this.max_max / 6 / this.level_divide.vip1
    }
    else if (this.user.growth >= this.level_divide.vip1 && this.user.growth < this.level_divide.vip2) {
      this.isvip1 = true,
      this.max = this.level_divide.vip2
      this.mygrowth = this.user.growth * this.max_max / 6 / this.level_divide.vip2 + this.max_max / 6 * 1
    }
    else if (this.user.growth >= this.level_divide.vip2 && this.user.growth < this.level_divide.vip3) {
      this.isvip2 = true,
      this.max = this.level_divide.vip3
      this.mygrowth = this.user.growth * this.max_max / 6 / this.level_divide.vip3 + this.max_max / 6 * 2
    }
    else if (this.user.growth >= this.level_divide.vip3 && this.user.growth < this.level_divide.vip4) {
      this.isvip3 = true,
      this.max = this.level_divide.vip4
      this.mygrowth = this.user.growth * this.max_max / 6 / this.level_divide.vip4 + this.max_max / 6 * 3
    }
    else if (this.user.growth >= this.level_divide.vip4 && this.user.growth < this.level_divide.vip5) {
      this.isvip4 = true,
      this.max = this.level_divide.vip5,
      this.mygrowth = this.user.growth * this.max_max / 6 / this.level_divide.vip5 + this.max_max / 6 * 4
    }
    else if (this.user.growth >= this.level_divide.vip5 && this.user.growth < this.level_divide.vip6) {
      this.isvip5 = true,
      this.max = this.level_divide.vip6,
      this.mygrowth = this.user.growth * this.max_max / 6 / this.level_divide.vip6 + this.max_max / 6 * 5
    } else if (this.user.growth >= this.level_divide.vip6 && this.user.growth < this.level_divide.vip7) {
      this.isvip6 = true,
      this.max = this.level_divide.vip7,
      this.mygrowth = this.user.growth * this.max_max / 6 / this.level_divide.vip6 + this.max_max
    }
    this.surp = this.max - this.user.growth
    this.vip0 = this.isvip0 ? 'http://pbr2drfuy.bkt.clouddn.com/Vip1%20%289%29.png' : 'http://pbr2drfuy.bkt.clouddn.com/Vip1%20%288%29.png',
      this.vip1 = this.isvip1 ? 'http://pbr2drfuy.bkt.clouddn.com/Vip2%20%283%29.png' : 'http://pbr2drfuy.bkt.clouddn.com/Vip2%20%282%29.png',
      this.vip2 = this.isvip2 ? 'http://pbr2drfuy.bkt.clouddn.com/Vip3%20%283%29.png' : 'http://pbr2drfuy.bkt.clouddn.com/Vip3%20%282%29.png',
      this.vip3 = this.isvip3 ? 'http://pbr2drfuy.bkt.clouddn.com/Vip4%20%283%29.png' : 'http://pbr2drfuy.bkt.clouddn.com/Vip4%20%282%29.png',
      this.vip4 = this.isvip4 ? 'http://pbr2drfuy.bkt.clouddn.com/vip5%20%281%29.png' : 'http://pbr2drfuy.bkt.clouddn.com/vip5.png',
      this.vip5 = this.isvip5 ? 'http://pbr2drfuy.bkt.clouddn.com/Vip6%20%281%29.png' : 'http://pbr2drfuy.bkt.clouddn.com/Vip6%20%282%29.png',
      this.vip6 = this.isvip6 ? 'http://pbr2drfuy.bkt.clouddn.com/Vip7%20%281%29.png' : 'http://pbr2drfuy.bkt.clouddn.com/Vip7.png'
  },
})